<#import "../common/layout.ftl" as layout/>
<#import "../common/dataTables.ftl" as dataTables/>
<#import "../common/modern-forms.ftl" as mform/>
<#import "../common/modal.ftl" as modal/>
<!DOCTYPE html>
<html>
<@layout.head title="仓库管理">
    <@dataTables.head/>
    <@dataTables.buttons_head/>
    <@dataTables.select_head/>
    <@dataTables.fixedHeader_head/>
    <@mform.head/>
    <@select.head/>
</@layout.head>
<body>
<@layout.page>
<div class="main-box">
    <div class="content">
        <div id="searchGroup" class="btn-group" style="position: absolute; ">
            <div style='float:left;margin:5px' id="warehouseBox">

            </div>
            <div style='float:left;margin:5px'>
                <button id="btnAddTopMenu" type="button" class="btn btn-blue btn-sm"
                        style="margin-left: 5px;"
                        onclick="javascript:refreshData1();">
                    <i class="fa fa-search"></i> 查询资产类型总量
                </button>
            </div>
        </div>
        <!-- 表格 -->
        <div style="position: relative; top:40px; display: flex">
            <div style="flex: 1;min-width:450px">
                <@dataTables.commonTable id="mainTable"/>
            </div>
            <div style="position: relative;flex: 2;margin-left: 20px;">
                <div style="position: absolute;    top: 10px;color: #2196f3;">点击资产类型总量表查询不同型号的库存情况</div>
                <@dataTables.commonTable id="mainTable2"/>
            </div>
        </div>
        <!-- 按钮 -->
        <div id="btnGroup" class="btn-group" style="position: absolute; top:40px;">
            <button id="btnAddTopMenu" type="button" class="btn btn-blue btn-sm" style="margin-left: 5px;"
                    onclick="javascript:newdetail();">
                <i class="fa fa-plus"></i> 添加
            </button>

            <button id="btnAddTopMenu" type="button" class="btn btn-blue btn-sm" style="margin-left: 5px;"
                    onclick="javascript:editdetail();">
                <i class="fa fa-edit"></i> 修改
            </button>
            <button id="btnAddTopMenu" type="button" class="btn btn-blue btn-sm" style="margin-left: 5px;"
                    onclick="javascript:deldetail();">
                <i class="fa fa-minus"></i> 删除
            </button>

        </div>
    </div>
</div>
<!-- 弹出窗 -->
    <@modal.editModal id="modal-detail" buttonId="btndetailSave" width="600px" >
        <@mform.container id="editForm">
        <input id="id" name="id" type="hidden"/>
        <input id="warehouseId" name="warehouseId" type="hidden"/>
            <@mform.select2 id='oneCode' name='oneCode' label="资产类型"  size=12>
            <option value="">请选择资产</option>
                <#list eqClass as item>
                <option value="${item.code}">${item.name}</option>
                </#list>
            </@mform.select2>
            <@mform.select2 id='brandCode' name='brandCode' label="品牌"  size=12>
            <option value="">请选择品牌</option>
                <#list brand as item>
                <option value="${item.code}">${item.name}</option>
                </#list>
            </@mform.select2>
            <@mform.select2 id='model' name='model' label="型号名称"  size=12>
            <option value="">请选择型号</option>
            </@mform.select2>
            <@mform.input_text id='count' name='count' size=12 label='当前数量' placeholder='当前库存数量' icon='fa-edit' />
            <@mform.input_text id='unit' name='unit' size=12 label='单位' placeholder='以最小领用单位为准' icon='fa-edit' />
            <@mform.input_text id='warnCount' name='warnCount' size=12 label='报警数量' placeholder='少于此数量将会报警' icon='fa-edit' />
        </@mform.container>
    </@modal.editModal>


</@layout.page>
<@layout.foot>
    <@dataTables.foot/>
    <@dataTables.buttons_foot/>
    <@dataTables.select_foot/>
    <@dataTables.fixedHeader_foot/>
    <@mform.foot/>
<script src="${request.contextPath}/assets/js/jquery.serializejson.min.js?v=3653060703"></script>
<script type="text/javascript">
    refreshWarehouse();
    var mainTable = $('#mainTable').winningTable({
        columns: [
            {'sWidth': '100px', 'title': '资产类型', 'data': 'name'},
            {'sWidth': '50px', 'title': '库存数量', 'data': 'count'},
            {'sWidth': '50px', 'title': '警告数量', 'data': 'warnCount'},
//            {'sWidth': '300px', 'title': '申请中数量', 'data': 'count'}
        ],
        usePager: false,
        "paging": false,
        pageLength: 10,
        ordering: false,
        "rowCallback": function (row, data, index) {
//            $('td', row).eq(4).html(data.eqCount+ "人");
        }
    });
    var mainTable2 = $('#mainTable2').winningTable({
        columns: [
            {'sWidth': '100px', 'title': '资产分类', 'data': 'typeName'},
            {'sWidth': '100px', 'title': '品牌', 'data': 'brandName'},
            {'sWidth': '200px', 'title': '型号名称', 'data': 'name'},
            {'sWidth': '100px', 'title': '库存数量', 'data': 'count'},
            {'sWidth': '100px', 'title': '单位', 'data': 'unit'},
            {'sWidth': '100px', 'title': '报警数量', 'data': 'warnCount'},
//            {'sWidth': '300px', 'title': '申请中数量', 'data': 'count'}
        ],
        usePager: false,
        "paging": false,
        pageLength: 10,
        "rowCallback": function (row, data, index) {
//            $('td', row).eq(4).html(data.eqCount+ "人");
        }
    });

    // 仓库查询
    function refreshWarehouse() {
        $.ajax({
            type: 'POST',
            data: { },
            url: "${request.contextPath}/interface/getMyWarehouse",
            success: function (data) {
                var data=data.datas;
                //成功之后
                var boxs = $("#warehouseBox");
                boxs.html("");
                if (data.length > 0) {
                    //动态加载下拉框
                    var select = $("<select class='form-control' id='warehouseSelect'> <option value=0>全部仓库</option> </select>");
                    boxs.append(select);
                    for (var i = 0; i < data.length; i++) {
                        select.append("<option value='" + data[i].id + "'>" + data[i].name+ "</option>");
                    }
                    select.select2();
                }
            },
            error: function (xhr, type) {
                swal("错误", "系统发生内部错误!请稍后再试!", "error");
            }
        });
    }

    //==================================================end======================================================================================
    function getWarehouseId(){
        if($("#oneOrganization").val()==0){
            return null;
        }else{
            if($("#warehouseSelect").length==0){
                return "0";
            }
            if($("#warehouseSelect").val()=="0"){
                var value="";
                for(var i=0;i<$("#warehouseSelect option").length;i++){
                    value+=(","+$("#warehouseSelect option").eq(i).attr("value"));
                }
                return value.substr(1);
            }else{
                return $("#warehouseSelect").val();
            }
        }
    }
    // 刷新1
    function refreshData1() {
        if ($("#oneOrganization").val()==0){
            swal("错误", "请选择机构", "error");
            return;
        }
        mainTable.queryData("${request.contextPath}/busi/warehouseMain/queryEqClassList",{
            datas: JSON.stringify({
                warehouseId:getWarehouseId()
            })
        });
    }
    // 刷新2
    function refreshData2(id) {
        if(id==null){
            var datas = mainTable.getSelectedDatas();
            if(datas.length==0) return ;
            else id=datas[0].id
        }
        mainTable2.queryData("${request.contextPath}/busi/warehouseMain/queryList",{
            datas: JSON.stringify({
                id:id,
                warehouseId:getWarehouseId()
            })
        });
    }

    $('#mainTable').on("click","tr",function(){
        var index=$(this).index();
        if($(this).hasClass("selected"))
            refreshData2(mainTable.datas[index].id)
    });
    // 新增
    function newdetail() {
        if($("#warehouseSelect").length==0||$("#warehouseSelect").val()==0){
            swal("错误", "请选择仓库", "error");
            return;
        }
        $('#modal-detail').find(".modal-title").text('为【'+$("#warehouseSelect option:selected").html()+'】添加存储中的资产');
        clearForm($('#modal-detail'));
        $("#modal-detail #warehouseId").val($("#warehouseSelect").val());
        $("#btndetailSave").unbind();
        $("#btndetailSave").click(savedetail);
        $('#modal-detail').modal();
    }
    // 修改
    function editdetail() {
        var datas = mainTable2.getSelectedDatas();
        if (datas == null || datas.length == 0 || datas.length > 1) {
            swal("错误", "请选择一行资产型号数据", "error");
            return;
        }
        var $modal = $("#modal-detail");
        $modal.find(".modal-title").text("修改信息");
        clearForm($modal);
        var data = datas[0];
        initFormItemData($("#editForm"), data);
        loadModel(data.model);
        $modal.modal();
        $("#btndetailSave").unbind();
        $("#btndetailSave").click(savedetail);
    }

    $("#brandCode").bind("change", loadModel);
    $("#oneCode").bind("change", loadModel);
    function loadModel(initValue) {
        $.ajax({
            type: 'POST',
            data: {
                datas: JSON.stringify({
                    oneCode: $("#oneCode").val(),
                    brandCode: $("#brandCode").val()
                })
            },
            url: '${request.contextPath}/busi/equipment/use/queryModel',
            success: function (data) {
                //成功之后
                $("#model").children().remove();
                var optionDefault = $("<option value='0'>请选择</option>");
                $("#model").append(optionDefault);
                if (data.success) {
                    $(data.datas).each(function (index, item) {
                        var option = $("<option value='" + item.id + "'>" + item.name + "-" + item.code + "</option>");
                        $("#model").append(option);
                    })
                    $("#model").val(initValue);
                    $('#select2-model-container').html($("#model").find("option:selected").text());
                }
                else {
                    swal("错误", data.errorMsg, "error");
                }
            },
            error: function (xhr, type) {
                swal("错误", "系统发生内部错误!请稍后再试!", "error");
            }
        })
    }

    // 保存
    function savedetail() {
        swal({
            title: "确定保存",
            text: "是否确定保存此数据",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: "是的,确定",
            cancelButtonText: "取消"
        }).then(function (result) {
            if (!result.value)
                return;
            var requestData = $("#modal-detail form").serializeJSON();
            if(requestData.model==null||requestData.model=="") {
                swal("错误", "请选择有效的型号！", "error");
                return;
            }
            var params = {datas: JSON.stringify(requestData)};
            $.ajax({
                type: 'POST',
                data: params,
                url: '${request.contextPath}/busi/warehouseMain/save',
                success: function (data) {
                    //成功之后
                    if (data.success) {
                        swal({
                            title: "成功",
                            text: "操作成功!",
                            type: "success",
                            showCancelButton: false,
                            confirmButtonText: "确定"
                        }).then(function (result) {
                            $("#modal-detail").modal('hide');
                            refreshData2();
                            refreshData1();
                        });
                    } else {
                        swal("错误", data.errorMsg, "error");
                    }
                },
                error: function (xhr, type) {
                    swal("错误", "系统发生内部错误!请稍后再试!", "error");
                }
            });
        });
    }

    // 删除
    function deldetail() {
        var datas = mainTable2.getSelectedDatas();
        if (datas == null || datas.length == 0 || datas.length > 1) {
            swal("错误", "请选择一行资产型号数据", "error");
            return;
        }
        var data = datas[0];
        swal({
            title: "确定删除",
            text: "是否确定删除此数据",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: "是的,确定",
            cancelButtonText: "取消"
        }).then(function (result) {
            if (!result.value)
                return;
            var requestData = {
                id: data.id
            };
            var params = {datas: JSON.stringify(requestData)};
            $.ajax({
                type: 'POST',
                data: params,
                url: '${request.contextPath}/busi/warehouseMain/delete',
                success: function (data) {
                    //成功之后
                    if (data.success) {
                        swal({
                            title: "成功",
                            text: "操作成功!",
                            type: "success",
                            showCancelButton: false,
                            confirmButtonText: "确定"
                        }).then(function (result) {
                            refreshData2();
                            refreshData1();
                        });
                    } else {
                        swal("错误", data.errorMsg, "error");
                    }
                },
                error: function (xhr, type) {
                    swal("错误", "系统发生内部错误!请稍后再试!", "error");
                }
            });
        });
    }

</script>
</@layout.foot>
</body>
</html>